package com.qire.manhua.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qire.antsbinder.dal.exception.DataAccessException;
import com.qire.antscore.annotation.DalExceptionObserve;
import com.qire.antscore.annotation.RouteMapping;
import com.qire.antsrouter.inject.Autowired;
import com.qire.common.constant.Url;
import com.qire.common.inject.EventControllerInjector;
import com.qire.common.utils.ObserveBinderHelper;
import com.qire.common.utils.ToastHelper;
import com.qire.manhua.R;
import com.qire.common.constant.Navigation;
import com.qire.common.support.base.BaseFragment;
import com.qire.manhua.controller.ClassificationController;
import com.qire.manhua.databinding.FragmentClassificationBinding;
import com.qire.manhua.model.enumeration.PageTabIndex;
import com.qire.manhua.model.viewModel.impl.ClassificationViewModel;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

@RouteMapping(path = Navigation.Fragment.ClassificationFragment)
public class ClassificationFragment extends BaseFragment<FragmentClassificationBinding> {

    @Autowired
    int pageNo = PageTabIndex.TAB_INDEX_INVALID;

    @Autowired
    ClassificationViewModel viewModel;

    @Autowired(injector = EventControllerInjector.class)
    ClassificationController controller;

    @Override
    public int layoutRid() {
        return R.layout.fragment_classification;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        registerObserver();
        FragmentClassificationBinding binder = viewDataBinder(inflater,container);
        binder.setClassificationViewModel(viewModel);

        viewModel.onAddition(controller);
        controller.onAddition(viewModel);

        viewModel.loadAllClassifiedTag();

        return binder.getRoot();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unregisterObserver();
    }

    @Override
    public void onResume() {
        super.onResume();
        togglePageIfNeed();
    }

    private void togglePageIfNeed() {
        // NOTE: 跳转页面后重置选项卡索引，避免重复进入，仅在第一次或其他页面跳转时注入索引才触发跳转子页
        if(pageNo != PageTabIndex.TAB_INDEX_INVALID) {
            viewModel.gotoTagTabBy(String.valueOf(pageNo));
            pageNo = PageTabIndex.TAB_INDEX_INVALID;
        }
    }

    protected void registerObserver() {
        ObserveBinderHelper.callRegisterOfDalExceptionObserveBinder(this);
    }

    protected void unregisterObserver() {
        ObserveBinderHelper.callUnregisterOfDalExceptionObserveBinder(this);
    }

    @DalExceptionObserve(tags = Url.HomeClassificationComicList)
    public void fetchClassifiedComicListExceptionHandle(DataAccessException e) {
        viewModel.refreshLayoutFinish(false);
        viewModel.refreshEmptyDataUI();

        ToastHelper.showToast(e.extraMsg);
    }

}
